<?php

class Model_ServiceInfinityads extends Model_Service
{

	public function loginCheck($dpt_id, $dpt_account_login, $password="")
	{
		$service_info = $this->getServiceInfo($dpt_id);
			
		$dbconn = $this->getServiceConn($dpt_id);
		//Zend_Debug::dump($dbconn);
		//Zend_Debug::dump($service_info);
		if (isset($dbconn['admin']) )
		{
			$account_id = $dpt_account_login;
			$account_info = explode('-',$dpt_account_login); // 'networkid-P-accountid'
			
			if (count($account_info) != 1 && count($account_info) != 3 && ( (count($account_info)==3)and(strtoupper($account_info[1])!='P')) && ( (count($account_info)==3)and(strtoupper($account_info[1])!='A')) )
			{
				$rs['status'] = false;
				$rs['message'] = 'account format error';
			}else{				
				if (count($account_info) == 1) $account_type ='ADMIN';
        		else 
        		{
          			$account_type = strtoupper($account_info[1]);
          			$account_id = $account_info[2];
        		}
						
			  	if ($account_type=='P'){
	        		$select  = $dbconn['admin']->select()->from('users');
					$select->where('userID = ?', (int)$account_id);
			  	}else if ($account_type=='A'){
	        		$select  = $dbconn['admin']->select()->from('advertisers');
					$select->where('adsid = ?', (int)$account_id);
			  	}else{
          			$select  = $dbconn['admin']->select()->from('admins');
          			$select->where('aid = ?', $dpt_account_login);                   
        		}					
				
				$user = $dbconn['admin']->fetchRow($select);
				if (isset($account_info[1]) && $account_type=='P'){
					$passwd = md5($password);
				}else{
					$passwd = $password;
				}
					
				$status = $user['status'];
				$passwd_stored = $user['password'];
					
				if (!$user)
				{
					$rs['status'] = false;
					$rs['message'] = 'account not found';
				}
				elseif (($status=='Suspend') || ($status=='Disable') || ($status=='Cancel'))
				{
		        	$rs['status'] = false;
            		$rs['message'] = 'account not active';
				}
				elseif ($password && $passwd !=$passwd_stored)
				{
		        	$rs['status'] = false;
            		$rs['message'] = 'wrong password';
				}else{
		        	$rs['status'] = true;
					if ($account_type=='P'){
            			$rs['dpt_account_id'] = $user['userID'];
						$rs['login_area'] = 'publisher';
					}elseif ($account_type=='A'){
            			$rs['dpt_account_id'] = $user['adsid'];
						$rs['login_area'] = 'advertiser';
					}elseif ($account_type=='ADMIN') {
                  		$rs['dpt_account_id'] = $user['admin_id'];
                  		$rs['login_area'] = 'admin';                
              		}
					$rs['email'] = $user['email'];
            		
           	 		$rs['dpt_account_login'] = $dpt_account_login;            
				}
			}
			
		}
		else
		{
			$rs['status'] = false;
     		$rs['message'] = 'service connection error';
		}

		return $rs; // return OK if passed
	}

	
	public function createAccount($user_id, $dpt_id, $profile)
	{
		$password = Model_User::str_makerand(6,8); //create a random password for remote account
		
		$params=array();
		$params['password']=$password;

		if (is_array($profile)){
			$name = $profile['first_name'].' '.$profile['last_name'];
			
			$params['name']=$name;
			foreach($profile as $key => $value){
				switch($key){
					case 'email':
					case 'phone':
					case 'address':
					case 'city':
					case 'state':
					case 'zipcode':
					case 'country':
					case 'website':
						if (!empty($value)) $params[ $key ] = $value;
						break;
					default: break;
				}
			}
			
		}
		$area = isset($profile['area']) ? $profile['area'] : 'publisher';  //for account type:advertiser,publisher,admin
		$params['area'] = $area;
		
		$remote_call = 'http://member.infinityads.com/server-rpc';
		$client = new Zend_XmlRpc_Client($remote_call);		
		$account_id = $client->call("YesupAcc.addUser",array($params));
		
		if ($account_id>0){
			if ($area=='publisher')
				$login_account = '5-P-'.$account_id;
			else 
				$login_account = '5-A-'.$account_id;
			
			$dpt_login_arr=array();
			$dpt_login_arr['dpt_account_login'] = $login_account;
			$dpt_login_arr['login_area'] = $area;
			$dpt_login_arr['email'] = $profile['email'];
			$dpt_login_arr['password'] = $password;
			$dpt_login_arr['admin_user_id'] = '';
			$dpt_login_arr['url'] = $_SERVER['REQUEST_URI'];
			$dpt_account_id = $account_id;
			
			$model_client = new Model_UserClient();
			$result = $model_client->linkClientService($user_id, $dpt_id, $dpt_account_id, $dpt_login_arr);
			if ($result){
				$ss = new Zend_Session_Namespace('service');
				$ss->link_result = 'success';
				$ss->link_dpt_id = $dpt_id;
				$ss->link_login_account = $login_account;
				$ss->link_dpt_account_id = $account_id;
				
				return true;
			}else 
				return false;			
		}else{
			return false;
		}
		
	}
	
}